package com.agent.web;

import com.agent.entity.User;
import com.agent.page.EasyuiPage;
import com.agent.service.ILeaveMsgService;
import com.agent.service.UserService;
import com.agent.shiro.ShiroUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/leaveMsg")
@Slf4j
public class LeaveMsgController {

    @Autowired
    ILeaveMsgService leaveMsgService;

    @Autowired
    UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String toHtml(){
        return "callIn/leaveMsg";
    }



    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    @RequiresRoles(value = {"售后坐席","班长座席"},logical = Logical.OR)
    public EasyuiPage list(Integer page, Integer rows) {
        try{
            Map<String,String > map = new HashMap<>();
            ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
            if(shiroUser.getRoleId() == 2){ // 普通坐席
                Map<String,String > in = new HashMap<>();
                in.put("userName",shiroUser.getUserName());
                User user = userService.findOne(in);
                if(user !=null){
                    map.put("OriAni",user.getExtension());
                }else {
                    return new EasyuiPage();
                }
            }
            return leaveMsgService.list(page,rows,map);
        }catch (Exception e ){
            log.error("com.agent.web.LeaveMsgController.list |",e);
        }
        return new EasyuiPage();

    }
}
